<?php

/**
 * User form base class.
 *
 * @method User getObject() Returns the current form's model object
 *
 * @package    maximous
 * @subpackage form
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseUserForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                        => new sfWidgetFormInputHidden(),
      'role_id'                   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Role'), 'add_empty' => false)),
      'username'                  => new sfWidgetFormInputText(),
      'name'                      => new sfWidgetFormInputText(),
      'password_request'          => new sfWidgetFormInputText(),
      'password_request_token'    => new sfWidgetFormInputText(),
      'password_request_end_date' => new sfWidgetFormDateTime(),
      'token'                     => new sfWidgetFormInputText(),
      'password'                  => new sfWidgetFormInputText(),
      'last_login'                => new sfWidgetFormDateTime(),
      'email'                     => new sfWidgetFormInputText(),
      'code'                      => new sfWidgetFormInputText(),
      'phone'                     => new sfWidgetFormInputText(),
      'mobile'                    => new sfWidgetFormInputText(),
      'address'                   => new sfWidgetFormInputText(),
      'photo'                     => new sfWidgetFormInputText(),
      'need_check'                => new sfWidgetFormInputCheckbox(),
      'statues'                   => new sfWidgetFormChoice(array('choices' => array(1 => 1, 2 => 2, 3 => 3))),
      'remember_me_code'          => new sfWidgetFormInputText(),
      'created_at'                => new sfWidgetFormDateTime(),
      'updated_at'                => new sfWidgetFormDateTime(),
      'deleted_at'                => new sfWidgetFormDateTime(),
      'best_categories_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Category')),
      'role_categories_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Category')),
      'category_list'             => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Category')),
    ));

    $this->setValidators(array(
      'id'                        => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'role_id'                   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Role'))),
      'username'                  => new sfValidatorString(array('max_length' => 255)),
      'name'                      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'password_request'          => new sfValidatorInteger(),
      'password_request_token'    => new sfValidatorString(array('max_length' => 255)),
      'password_request_end_date' => new sfValidatorDateTime(),
      'token'                     => new sfValidatorString(array('max_length' => 255)),
      'password'                  => new sfValidatorString(array('max_length' => 255)),
      'last_login'                => new sfValidatorDateTime(),
      'email'                     => new sfValidatorString(array('max_length' => 255)),
      'code'                      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'phone'                     => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'mobile'                    => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'address'                   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'photo'                     => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'need_check'                => new sfValidatorBoolean(array('required' => false)),
      'statues'                   => new sfValidatorChoice(array('choices' => array(0 => 1, 1 => 2, 2 => 3))),
      'remember_me_code'          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'created_at'                => new sfValidatorDateTime(),
      'updated_at'                => new sfValidatorDateTime(),
      'deleted_at'                => new sfValidatorDateTime(array('required' => false)),
      'best_categories_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Category', 'required' => false)),
      'role_categories_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Category', 'required' => false)),
      'category_list'             => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Category', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorDoctrineUnique(array('model' => 'User', 'column' => array('username'))),
        new sfValidatorDoctrineUnique(array('model' => 'User', 'column' => array('email'))),
      ))
    );

    $this->widgetSchema->setNameFormat('user[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'User';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['best_categories_list']))
    {
      $this->setDefault('best_categories_list', $this->object->BestCategories->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['role_categories_list']))
    {
      $this->setDefault('role_categories_list', $this->object->RoleCategories->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['category_list']))
    {
      $this->setDefault('category_list', $this->object->Category->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveBestCategoriesList($con);
    $this->saveRoleCategoriesList($con);
    $this->saveCategoryList($con);

    parent::doSave($con);
  }

  public function saveBestCategoriesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['best_categories_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->BestCategories->getPrimaryKeys();
    $values = $this->getValue('best_categories_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('BestCategories', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('BestCategories', array_values($link));
    }
  }

  public function saveRoleCategoriesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['role_categories_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->RoleCategories->getPrimaryKeys();
    $values = $this->getValue('role_categories_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('RoleCategories', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('RoleCategories', array_values($link));
    }
  }

  public function saveCategoryList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['category_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Category->getPrimaryKeys();
    $values = $this->getValue('category_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Category', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Category', array_values($link));
    }
  }

}
